package com.ruoyi.edu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.edu.domain.EduCourse;
import com.ruoyi.edu.domain.vo.EduCourseVo;
import com.ruoyi.edu.mapper.EduCourseMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.edu.domain.bo.EduSubjectBo;
import com.ruoyi.edu.domain.vo.EduSubjectVo;
import com.ruoyi.edu.domain.EduSubject;
import com.ruoyi.edu.mapper.EduSubjectMapper;
import com.ruoyi.edu.service.IEduSubjectService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 课程科目Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-18
 */
@RequiredArgsConstructor
@Service
public class EduSubjectServiceImpl implements IEduSubjectService {

    private final EduSubjectMapper baseMapper;
    private final EduCourseMapper eduCourseMapper;

    /**
     * 查询课程科目
     */
    @Override
    public EduSubjectVo queryById(String id){
        return baseMapper.selectVoById(id);
    }



    /**
     * 查询课程科目列表
     */
    @Override
    public List<EduSubjectVo> queryList(EduSubjectBo bo) {
        LambdaQueryWrapper<EduSubject> lqw = buildQueryWrapper(bo);
        List<EduSubjectVo> eduSubjectVos = baseMapper.selectVoList(lqw);
        List<EduSubjectVo> collect = eduSubjectVos.stream().map(item -> {

            EduSubject eduSubject1 = baseMapper.selectById(item.getId());
            item.setParentId(String.valueOf(eduSubject1.getParentId()));
            return item;
        }).collect(Collectors.toList());

        return collect;
    }

    private LambdaQueryWrapper<EduSubject> buildQueryWrapper(EduSubjectBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EduSubject> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTitle()), EduSubject::getTitle, bo.getTitle());
        return lqw;
    }

    /**
     * 新增课程科目
     */
    @Override
    public Boolean insertByBo(EduSubjectBo bo) throws Exception {
        EduSubject add = BeanUtil.toBean(bo, EduSubject.class);
        if(bo.getParentId() != 0){
            validEntityBeforeSave(add);
        }
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改课程科目
     */
    @Override
    public Boolean updateByBo(EduSubjectBo bo) throws Exception {
        EduSubject update = BeanUtil.toBean(bo, EduSubject.class);
        String id = bo.getId();
        String string = bo.getParentId().toString();
        if(id.equals(string) ){
            throw new Exception("你知道你在干啥吗你，自己当自己子类是吧,建议抽空看看脑子");
        }
        if(bo.getParentId() != 0) {
            validEntityBeforeSave(update);
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EduSubject entity) throws Exception {
        //TODO 做一些数据校验,如唯一约束

//        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("id", entity.getParentId());
//        List<EduSubject> eduSubjects = baseMapper.selectList(queryWrapper);
//        for (EduSubject eduSubject :eduSubjects){
        EduSubject eduSubject = baseMapper.selectById(entity.getParentId());
        if(eduSubject.getParentId() !=0){
                //TODO 做一些数据校验,如唯一约束
                throw new Exception("该级别已是最低级别，无法继续添加下级分类");
            }
//        }


    }

    /**
     * 批量删除课程科目
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) throws Exception {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
           for(String aa :ids){
               QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
               queryWrapper.eq("subject_id",aa);
               List<EduCourseVo> eduCourseVos = eduCourseMapper.selectVoList(queryWrapper);
               if(eduCourseVos.size() !=0){
                   throw new Exception("(╬￣皿￣)=○ 别瞎删，下边有课呢凸(艹皿艹 )");
               }
           }
        }
        int i = baseMapper.deleteBatchIds(ids);
        return i > 0;
    }

    @Override
    public List<EduSubjectVo> queryByFId(String id) {
        final List<EduSubjectVo> list = baseMapper.selectVoList(new QueryWrapper<EduSubject>().eq("parent_id", id));

        return list;
    }
}
